﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Framework
{
    public class SocketHelper
    {
        public Action<object, System.Timers.ElapsedEventArgs> SpeedProgress;
        public IPAddress Ip { get; set; }
        public int Port;
        public string StringIp
        {
            set
            {
                Ip = IPAddress.Parse(value);
            }
        }
        /// <summary>
        /// 打开端口监听
        /// </summary>
        public void OpentTcpListener()
        {
            var tcpListener = new TcpListener(Ip, Port);
            tcpListener.Start();
        }
        /// <summary>
        /// 打开端口监听
        /// </summary>
        public static void OpentTcpListener(string ip, int port)
        {
            var tcpListener = new TcpListener(IPAddress.Parse(ip), port);
            tcpListener.Start();
        }
        public void Connect()
        {
            var tcpClient = new TcpClient();
            tcpClient.Connect(Ip, Port);
            // 打印连接到的服务端信息
            Console.WriteLine("Server Connected！{0} --> {1}",
                tcpClient.Client.LocalEndPoint, tcpClient.Client.RemoteEndPoint);
        }
        public static TcpClient Connect(string ip, int port)
        {
            var tcpClient = new TcpClient();
            tcpClient.Connect(ip, port);
            // 打印连接到的服务端信息
            Console.WriteLine("Server Connected！{0} --> {1}",
                tcpClient.Client.LocalEndPoint, tcpClient.Client.RemoteEndPoint);
            return tcpClient;
        }
        public static void Send(string ip, int port, string message)
        {
            var client = new TcpClient();
            client.Connect(IPAddress.Parse(ip), port);      // 与服务器连接  
            NetworkStream streamToServer = client.GetStream();
            byte[] buffer = Encoding.Unicode.GetBytes(message);     // 获得缓存
            streamToServer.Write(buffer, 0, buffer.Length);     // 发往服务器
        }
        public void Service()
        {
            const int BufferSize = 8192;    // 缓存大小，8192字节

            Console.WriteLine("Server is running ... ");
            var listener = new TcpListener(Ip, Port);
            listener.Start();           // 开始侦听
            Console.WriteLine("Start Listening ...");
            TcpClient remoteClient = listener.AcceptTcpClient();
            // 打印连接到的客户端信息
            Console.WriteLine("Client Connected！{0} <-- {1}",
                remoteClient.Client.LocalEndPoint, remoteClient.Client.RemoteEndPoint);
            // 获得流，并写入buffer中
            NetworkStream streamToClient = remoteClient.GetStream();
            var buffer = new byte[BufferSize];
            int bytesRead = streamToClient.Read(buffer, 0, BufferSize);
            Console.WriteLine("Reading data, {0} bytes ...", bytesRead);
            // 获得请求的字符串
            string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Received: {0}", msg);
        }
    }
}
